package cn.tellsea.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 金额计算工具类
 *
 * @author Tellsea
 * @date 2021/12/31
 */
public class BigDecimalUtils {

    /**
     * 默认保存2位小数点
     */
    private static int POINTS = 2;

    /**
     * 默认进位方式
     */
    private static RoundingMode MODE = RoundingMode.CEILING;


    /**
     * 加
     *
     * @param a
     * @param b
     * @return
     */
    public static BigDecimal add(BigDecimal a, BigDecimal b) {
        if (a == null) {
            a = BigDecimal.valueOf(0.00);
        }
        if (b == null) {
            b = BigDecimal.valueOf(0.00);
        }
        return computer(1, a, b, POINTS, MODE);
    }

    public static BigDecimal add(BigDecimal a, BigDecimal b, int points, RoundingMode mode) {
        if (a == null) {
            a = BigDecimal.valueOf(0.00);
        }
        if (b == null) {
            b = BigDecimal.valueOf(0.00);
        }
        return computer(1, a, b, points, mode);
    }

    /**
     * 减
     *
     * @param a
     * @param b
     * @return
     */
    public static BigDecimal subtract(BigDecimal a, BigDecimal b) {
        if (a == null) {
            a = BigDecimal.valueOf(0.00);
        }
        if (b == null) {
            b = BigDecimal.valueOf(0.00);
        }
        return computer(2, a, b, POINTS, MODE);
    }

    public static BigDecimal subtract(BigDecimal a, BigDecimal b, int points, RoundingMode mode) {
        if (a == null) {
            a = BigDecimal.valueOf(0.00);
        }
        if (b == null) {
            b = BigDecimal.valueOf(0.00);
        }
        return computer(2, a, b, points, mode);
    }


    /**
     * 乘
     *
     * @param a
     * @param b
     * @return
     */
    public static BigDecimal multiply(BigDecimal a, BigDecimal b) {
        return computer(3, a, b, POINTS, MODE);
    }

    /**
     * 除
     *
     * @param a
     * @param b
     * @return
     */
    public static BigDecimal divide(BigDecimal a, BigDecimal b) {
        if (b.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.valueOf(0.00);
        }
        return computer(4, a, b, POINTS, MODE);
    }

    public static BigDecimal divide(BigDecimal a, BigDecimal b, int points, RoundingMode mode) {
        return computer(4, a, b, points, mode);
    }

    /**
     * 计算方法
     *
     * @param type   1-加  2-减  3-乘  4-除
     * @param a
     * @param b
     * @param points
     * @param mode
     * @return
     */
    public static BigDecimal computer(int type, BigDecimal a, BigDecimal b, int points, RoundingMode mode) {
        BigDecimal rs;
        switch (type) {
            case 1:
                rs = a.add(b).setScale(points, mode);
                break;
            case 2:
                rs = a.subtract(b).setScale(points, mode);
                break;
            case 3:
                rs = a.multiply(b).setScale(points, mode);
                break;
            default:
                rs = a.divide(b, points, mode);
                break;
        }
        return rs;
    }


    public BigDecimal multiply(BigDecimal a, BigDecimal b, int points, RoundingMode mode) {
        return computer(3, a, b, points, mode);
    }

}
